package com.cxz.kotlin.baselibs.base

import android.content.Context
import android.graphics.PorterDuff
import android.graphics.drawable.Drawable
import android.text.Editable
import android.text.InputType
import android.text.TextWatcher
import android.view.Display
import android.view.View
import android.view.WindowManager
import android.widget.EditText
import com.cxz.kotlin.baselibs.utils.RxTimerUtil
import top.zibin.luban.Luban
import top.zibin.luban.OnCompressListener
import java.io.File


fun String.getSingleParmerJson(key: String): String {
    return "{\"$key\": \"$this\"}"
}

fun EditText.setTextChageListener(after: (text: String) -> Unit = {}){
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {

        }

        override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

        }

        override fun afterTextChanged(s: Editable?) {
            after(this@setTextChageListener.text.toString())
        }

    })
}

fun Int.toCountDown(
    callback: (time: Int) -> Unit = {
    }, finish: () -> Unit = {}
) {
    RxTimerUtil.interval(1000) {
        callback.invoke(this - (it).toInt())
        if (it >= this) {
            finish.invoke()
            RxTimerUtil.cancel()
        }
    }
}

fun String.subMystring(startStr: String = "", endStr: String = "", needReturn: Boolean = false): String {
    if (!this.contains(startStr) && !this.contains(endStr)) {
        return ""
    }
    val start = if (startStr.isEmpty()) {
        0
    } else {
        this.indexOf(startStr) + startStr.length
    }
    val end = if (endStr.isEmpty()) {
        this.length
    } else {
        this.indexOf(endStr)
    }
    return if (needReturn)"{${this.substring(start, end)}}" else this.substring(start, end)
}

fun EditText.setPSESee(view: View) {
    val see = view.getTag() as Boolean
    view.setTag(!see)
    view.isSelected = !see
    this.setInputType(
        if (see) InputType.TYPE_CLASS_TEXT
                or InputType.TYPE_TEXT_VARIATION_PASSWORD else InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
    )
    this.setSelection(this.text.length)
}

fun Context.getScreenheigth(): Int {
    return getDispaly(this).height
}
fun Context.getScreenWidth(): Int {
    return getDispaly(this).width
}

fun getDispaly(context: Context): Display{
    return (context.getApplicationContext().getSystemService(Context.WINDOW_SERVICE) as WindowManager).getDefaultDisplay()
}

fun String.getCombineStr(content: String, splite: String): String {
    if (this.isNullOrEmpty()) {
        return content
    } else {
        return "$this$splite$content"
    }
}

fun String.lubanCompress(context: Context,onSuccess:(file:String)->Unit = {}) {
    Luban.with(context)
        .load(this) // 传人要压缩的图片列表
        .ignoreBy(100) // 忽略不压缩图片的大小
        .setCompressListener(object : OnCompressListener {
            //设置回调
            override fun onStart() {
            }

            override fun onSuccess(file: File?) {
                file?.path?.let { onSuccess(it) }
            }

            override fun onError(e: Throwable) {
            }
        }).launch() //启动压缩


}

fun Int.changeDrawalbeColor(context: Context,color:Int): Drawable? {
    val upArrow = context.getResources().getDrawable(this)
    upArrow.setColorFilter(context.getResources().getColor(color), PorterDuff.Mode.SRC_ATOP)
    return upArrow
}

fun baiduToWgs84(bdLat: String, bdLon: String): Pair<Double, Double> {
    val PI = 3.14159265358979324
    val x_pi = 3.14159265358979324 * 3000.0 / 180.0
    val x = java.lang.Double.valueOf(bdLon) - 0.0065
    val y = java.lang.Double.valueOf(bdLat) - 0.006
    val z = Math.sqrt(x * x + y * y) - 0.00002 * Math.sin(y * x_pi)
    val theta = Math.atan2(y, x) - 0.000003 * Math.cos(x * x_pi)
    val gcjLon = z * Math.cos(theta)
    val gcjLat = z * Math.sin(theta)
    val a = 6378245.0
    val ee = 0.00669342162296594323
    var dLat: Double = transformLat(gcjLon - 105.0, gcjLat - 35.0)
    var dLon: Double = transformLon(gcjLon - 105.0, gcjLat - 35.0)
    val radLat = gcjLat / 180.0 * PI
    var magic = Math.sin(radLat)
    magic = 1 - ee * magic * magic
    val sqrtMagic = Math.sqrt(magic)
    dLat = dLat * 180.0 / (a * (1 - ee) / (magic * sqrtMagic) * PI)
    dLon = dLon * 180.0 / (a / sqrtMagic * Math.cos(radLat) * PI)
    dLat = gcjLat - dLat
    dLon = gcjLon - dLon
    return Pair(dLat,dLon)
}


fun transformLon(x: Double, y: Double): Double {
    val PI = 3.14159265358979324
    var ret = 300.0 + x + 2.0 * y + 0.1 * x * x + 0.1 * x * y + 0.1 * Math.sqrt(Math.abs(x))
    ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0
    ret += (20.0 * Math.sin(x * PI) + 40.0 * Math.sin(x / 3.0 * PI)) * 2.0 / 3.0
    ret += (150.0 * Math.sin(x / 12.0 * PI) + 300.0 * Math.sin(x / 30.0 * PI)) * 2.0 / 3.0
    return ret
}

fun transformLat(x: Double, y: Double): Double {
    val PI = 3.14159265358979324
    var ret = -100.0 + 2.0 * x + 3.0 * y + 0.2 * y * y + 0.1 * x * y + 0.2 * Math.sqrt(Math.abs(x))
    ret += (20.0 * Math.sin(6.0 * x * PI) + 20.0 * Math.sin(2.0 * x * PI)) * 2.0 / 3.0
    ret += (20.0 * Math.sin(y * PI) + 40.0 * Math.sin(y / 3.0 * PI)) * 2.0 / 3.0
    ret += (160.0 * Math.sin(y / 12.0 * PI) + 320 * Math.sin(y * PI / 30.0)) * 2.0 / 3.0
    return ret
}